(function() {
    Nokia.ImageDescription = Nokia.Widget.extend({
        initialize: function(options) {
            var instance = this;

			var defaults = {
				description: 'Image Description',
				lineHeight: 18,
				applyRoundedCorners: true,
				backgroundClass: 'ui-state-default',
				create: function() {},
				toggle: function() {}
            };

            instance.options = jQuery.extend(defaults, options);

            instance.element = jQuery( instance.options.element );

            instance._super.apply(instance, [instance.options]);

            instance.registerData("imagedescription");

            instance._create();
            instance._attachEvents();

			instance.registerVibrationOn([]);
			instance.fireCallback('create');
        },

        _attachEvents: function() {
        	var instance = this;
        	var element = instance.element;
        	var options = instance.options;
        	var Styles = instance.Styles;
        	var arrow = instance.arrow;
        	var arrowContainer = instance.arrowContainer;
        	var descriptionContainer = instance.descriptionContainer;
			var arrowContainer = instance.arrowContainer;

        	arrowContainer.toggle(
				function(e) {
					var height = 'auto';
	        		descriptionContainer.height(height);
	        		Nokia.util.removeClass(arrow[0], Styles.arrowDown);
	        		Nokia.util.addClass(arrow[0], Styles.arrowUp);
					Nokia.util.addClass(arrowContainer[0], Styles.open);

	        		instance.fireCallback('toggle');

					return false;
        		},
				function(e) {
					var height = 'auto';
					var lineHeight = parseInt(options.lineHeight, 10);

					if (options.maxLines) {
						height = lineHeight * options.maxLines;
					}

        			descriptionContainer.height(height);
	        		Nokia.util.addClass(arrow[0], Styles.arrowDown);
	        		Nokia.util.removeClass(arrow[0], Styles.arrowUp);
					Nokia.util.removeClass(arrowContainer[0], Styles.open);

	        		instance.fireCallback('toggle');

					return false;
        		}
			);

			Nokia.util.bindEnter(arrowContainer, function(e) {
				arrowContainer.trigger('click');
			});

        },

		_create: function() {
			var instance = this;
			var element = instance.element;
			var options = instance.options;
			var description = options.description;
			var Styles = instance.Styles;

			Nokia.util.addClass(element[0], Styles.image);

			var wrapper = Nokia.dom.parseHTML('<div class="' + [Styles.wrapper, options.backgroundClass].join(' ') + '"></div>');
			var descriptionContainer = Nokia.dom.parseHTML('<p class="' + Styles.description +'">' + description +'</p>');
			var arrowContainer = Nokia.dom.parseHTML('<a href="javascript:void(0);" class="' + Styles.arrowContainer +'"></a>');
			var arrow = Nokia.dom.parseHTML('<div class="' + [Styles.arrow, Styles.arrowDown].join(' ') +'"></div>');
			//var moreInfoElement = Nokia.dom.parseHTML('<span class="' + instance.Styles.moreInfo + '">...</span>');
			var width = element.outerWidth();

			instance.arrowContainer = arrowContainer;
			instance.descriptionContainer = descriptionContainer;
			instance.arrow = arrow;

			var height = 'auto';
			var lineHeight = parseInt(options.lineHeight, 10);

			if (options.maxLines) {
				height = lineHeight * options.maxLines;
			}

			//Nokia.dom.append(arrowContainer, moreInfoElement);
			Nokia.dom.append(arrowContainer, arrow);

			var wrapperInner = Nokia.dom.parseHTML('<div class="' + Styles.wrapperContainer + '"></div>');
			Nokia.dom.wrap(wrapper, element);
			Nokia.dom.wrap(wrapperInner, wrapper);

			Nokia.dom.insertAfter(element, arrowContainer);
			Nokia.dom.insertAfter(element, descriptionContainer);

			descriptionContainer.css({
				width: width,
				height: height,
				lineHeight: parseInt(options.lineHeight, 10) + 'px'
			});

			arrowContainer.css({
				width: width
			});

			if (options.applyRoundedCorners) {
				Nokia.util.applyRoundedCorners(element.parent());
			}
		},

		Styles: {
			image: 'nokia-imagedescription-element',
			open: 'nokia-open',
			wrapper: 'nokia-imagedescription-wrapper',
			wrapperContainer: 'nokia-imagedescription',
			arrowContainer: 'nokia-imagedescription-arrow-container',
			moreInfo: 'nokia-imagedescription-more-info',
			arrow: 'nokia-imagedescription-arrow',
			arrowDown: 'nokia-imagedescription-arrow-down',
			arrowUp: 'nokia-imagedescription-arrow-up',
			description: 'nokia-imagedescription-description',
			stateDeafult: 'ui-state-default'
		}
    });
})();